﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceRepository.DataModel.Mapping
{
    public class BindingMapper : BaseMapper<Binding,ServiceRepository.Domain.Binding>
    {
        protected override Domain.Binding FromData(Binding data)
        {
            return new Domain.Binding() { ID = data.ID, Default = data.Default, BindingType = (Domain.ServiceBindingType)data.BindingType, URI = data.URI };
        }

        protected override Binding FromDomain(Domain.Binding model)
        {
            Binding entity = (from obj in _ctx.Bindings
                         where obj.ID == model.ID select obj).FirstOrDefault();

            if(entity==null) {
                entity = new Binding();
                _ctx.Bindings.InsertOnSubmit(entity);
            }

            entity.BindingType = (int)model.BindingType;
            entity.Name = model.Name;
            entity.Default = model.Default;
            entity.URI = model.URI;

            return entity;

        }

        public List<Domain.Binding> GetServiceBindingsByServiceId(Service svc)
        {
            var bindings = (from q in _ctx.Bindings
                            where q.ServiceID == svc.ID
                            select FromData(q)).ToList();

            return bindings;
        }
    }
}
